package io.finer.erp.jeecg.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.service.PurchaseOrderLineService;
import io.finer.erp.jeecg.purchase.service.PurchaseOrderService;
import io.finer.erp.jeecg.stock.entity.StockMove;
import io.finer.erp.jeecg.stock.mapper.StockMoveMapper;
import io.finer.erp.jeecg.stock.service.StockMoveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.finer.erp.jeecg.stock.vo.PurchaseStockMove;
import io.finer.erp.jeecg.stock.vo.StockMovePage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * <p>
 * 库存移动 服务实现类
 * </p>
 *
 * @author yaoyifan
 * @since 2021-05-30
 */
@Service
public class StockMoveServiceImpl extends ServiceImpl<StockMoveMapper, StockMove> implements StockMoveService {

    @Autowired
    PurchaseOrderLineService purchaseOrderLineService;

    @Autowired
    PurchaseOrderService purchaseOrderService;

    public IPage<StockMovePage> selectStockMovePage(Page page, Wrapper<StockMovePage> queryWrapper) {
        return this.baseMapper.selectStockMovePage(page, queryWrapper);
    }

    @Override
    public StockMovePage selectStockMovePageById(String id) {
        return this.baseMapper.selectStockMovePageById(id);
    }

    @Override
    @Transactional
    public void purchaseStockMoveIn(List<PurchaseStockMove> purchaseStockMoveList) {
        Set<String> billNos = new HashSet<>();
        for(PurchaseStockMove purchaseStockMove: purchaseStockMoveList){
            StockMove stockMove = new StockMove();
            BeanUtils.copyProperties(purchaseStockMove, stockMove);
            this.baseMapper.updateById(stockMove);
            stockMove = this.baseMapper.selectById(purchaseStockMove.getId());
            if (purchaseStockMove.getStockInQty().compareTo(purchaseStockMove.getQty()) < 0){
                stockMove.setQty(purchaseStockMove.getStockInQty());
                StockMove newStockMove = new StockMove();
                BeanUtils.copyProperties(purchaseStockMove, newStockMove);
                newStockMove.setQty(purchaseStockMove.getQty().subtract(purchaseStockMove.getStockInQty()));
                newStockMove.setId(null);
                newStockMove.setState("可用");
                this.baseMapper.insert(newStockMove);
            }
            stockMove.setState("完成");
            this.baseMapper.updateById(stockMove);
            purchaseOrderLineService.updateInQtyState(stockMove.getRefId());
            billNos.add(stockMove.getOrigin());
        }
        for (String billNo : billNos) {
            purchaseOrderService.updateState(billNo);
        }
    }
}
